<template>
  <div>
    <div>
      <van-list v-model="loading"
                :finished="finished"
                :error.sync="error"
                error-text="请求失败，点击重新加载"
                finished-text=""
                @load="onLoad">
        <div class="coupon_list"
            v-for="(item,index) in transferRecords"
            :key="index">
          <div class="type_time">
            <div>转赠时间：{{item.created_time}}</div>
            <div class="type" :class="item.type == 1 ? '' : 'type_out'">{{item.type == 1 ? '转入' : '转出'}}</div>
          </div>
          <div class="type_time user_message">
            <div class="nickname">会员昵称：{{item.user_nickname}}</div>
            <div>会员ID：{{item.user_id}}</div>
          </div>
            <div>优惠券名称：{{item.coupon_name}}</div>
        </div>
      </van-list>
    </div>
    <div v-show="!transferRecords.length">
      <no-data :noData="noData"></no-data>
    </div>
  </div>
</template>

<script>
import { transferCouponRecords } from '@/api/user/member/memberApi'
import noData from '@/components/noData/index'
export default {
  name: 'index',
  components: {
    noData
  },
  data() {
    return {
      noData: {
        width: '49%',
        txtMarginTop: '-30px',
        img: 'https://img.wifenxiao.com/h5-wfx/images/no-coupon.png',
        text: ''
      },
      transferRecords: [],
      page: 1,
      total: 0,
      error: false,
      loading: false,
      finished: false
    }
  },
  mounted() {
    // 小程序触底加载更多
    window.addEventListener('reachbottom', () => {
      if (this.transferRecords.length < this.total) {
        this.onLoad()
      }
    })
  },
  methods: {
    init() {
      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 10000)
      this.page = 1
      this.onLoad()
    },
    onLoad() {
      const params = {
        p: this.page
      }
      transferCouponRecords(params)
        .then(res => {
          this.loading = false
          this.$loadingWrap.close()
          if (res.status == 1) {
            const resData = res.data
            this.total = resData.total
            // if (res.data.lists.length > 0) {
            //   if (this.p == 1) {
            //     this.transferRecords = resData.list
            //   } else {
            //     this.transferRecords.push(...resData.list)
            //   }
            //   this.params.p++
            // } else {
            //   this.finished = true
            // }
            if (this.p == 1) {
              this.transferRecords = resData.list
            } else {
              this.transferRecords.push(...resData.list)
            }
            if (resData.list.length < 10) {
              this.finished = true
            } else {
              this.page++
            }
            if (this.transferRecords.length == 0) {
              this.noData.text = '暂无转赠记录'
            }
          } else {
            this.$Error(res.msg)
          }
        })
        .catch(() => {
          this.error = true
        })
    }
  },
  created() {
    this.init()
  }
}
</script>

<style lang="scss">
.coupon_list {
  background: #fff;
  padding: 20px;
  margin: 20px 20px 0 20px;
  line-height: 50px;
  font-size: 26px;
  border-radius: 10px;
  div {
    white-space: inherit;
    word-break: break-all;
    text-overflow: ellipsis;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
  }
  .type_time {
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #f7f7f7;
    margin-bottom: 20px;
    padding-bottom: 10px;
    .type {
      // position: absolute;
      // right: 0;
      // top: 0;
      font-weight: bold;
      color: #4ab24c;
      text-align: right;
      &.type_out {
        color: #f70f28;
      }
    }
    &.user_message {
      justify-content: start;
      .nickname {
        width: 60%;
        margin-right: 20px
      }
    }
  }
}
</style>